<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ext_po extends MY_Controller {
	
	function __construct() {
		parent::__construct(); 
		
		$this->load->model('ext_po_model', 'Po');
		$this->load->model('ext_detail_po_model', 'Po_detail');
		$this->load->model('ext_combo_model', 'Combo');
		$this->secure_session();
	}

	function index() {
		$data['title'] = "Daftar Po";
		viewByTemplate('ext_po/index', $data, 'grid');
		// $this->edit();
	}

	function getdata() {
		$this->secure_session();
		echo $this->Po->GetJSON();
	}

	function create() {
		// Display Form Create
		$data['title'] = "Tambah Data Po";
		$data['data'] = NULL;
		
		// Define Combo Data
	    $data['listVendor'] = $this->Combo->listVendor();

		
		$data['mode'] = "create";
		viewByTemplate('ext_po/form', $data, 'form');
		// print_r($data['listVendor']);
	}

	function edit() {
		// Display Form Edit
		$data['listVendor'] = $this->Combo->listVendor();
		$data['title'] = "Ubah Data Po";
		$data['data'] = $this->Po->GetSingleView(get('id_po_pk'));

		// Define Combo Data
		$data['list_barang']=$this->Combo->listBarang();
		$data['list_satuan']=$this->Combo->listSatuan();

		
		// $data['file_allowed']		= implode(', ', $this->arr_allowed_upload);
		
		$data['mode'] = "edit";
		viewByTemplate('ext_po/form', $data, 'form');
	}

	function ajax_post() {
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try {
			if (count( $error->Get() ) > 0) { 
				$is_error = 1; 
			} else {
				// Fill Post Data
				$data	= post_by_key(array('id_po_pk', 'id_vendor_fk','tgl_po','tgl_jatuh_tempo','ket_po'));
				$data['id_po_pk']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['id_po_pk'])));

				// Set Date Format
				$data['tgl_po']	= ($data['tgl_po'] == '' ? NULL : date_format3($data['tgl_po'], 'YYYY-MM-DD'));
				$data['tgl_jatuh_tempo']	= ($data['tgl_jatuh_tempo'] == '' ? NULL : date_format3($data['tgl_jatuh_tempo'], 'YYYY-MM-DD'));

				// Set Number Format
				//$data['gaji_harapan']	= (float)toNumber($data['gaji_harapan']);
				
				if (post("mode") == "create") {
					$data['tgl_rekam']	= NOW;
					$data['user_rekam']	= USERID;

					$this->Po->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.app_id ?? ""));

				} else if (post("mode") == "edit") {
					$data['tgl_ubah']	= NOW;
					$data['user_ubah']	= USERID;
					
					$this->Po->Update($data, 
										array('id_po_pk'	=> $data['id_po_pk']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.app_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	private function validation() {
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		if (post("id_po_pk") == '') {
			$error->Add("id_po_pk", "Po Id", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM ext_po WHERE id_po_pk = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("id_po_pk"))))."'")) > 0) {
			$error->Add("id_po_pk", "Po Id", $tpl_duplicate);
		}

		if (post("tgl_po") == '') {
			$error->Add("tgl_po", "Tanggal PO", $tpl_empty);
		}

		if (post("id_vendor_fk") == '') {
			$error->Add("id_vendor_fk", "Nama Vendor", $tpl_empty);
		}

		// if (post("nama_pelamar") == '') {
		// 	$error->Add("nama_pelamar", "Po Name", $tpl_empty);
		// }

		return $error;
	}

	function ajax_delete() {
		$ls_result	= "";
		$id_po_pk	= get('id_po_pk');
		
		if ($id_po_pk != "") {
			// Delete Single
			$arr_rule	= array('id_po_pk' => $id_po_pk);
			$ls_result = $this->Po->DeleteSingle($arr_rule);

		} else {
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++) {
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_po_pk' => $row['id_po_pk']);
				$ls_result .= $this->Po->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}

}
?>